<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category StockController.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/19  17:49
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin\V1;

use App\Services\StockService;
use Illuminate\Http\Request;

class StockController extends Controller
{
    protected $stockService;

    public function __construct(StockService $stockService)
    {
        $this->stockService = $stockService;
    }

    public function index(Request $request)
    {
        $rules      = [
            'page'     => ['nullable', 'numeric', 'gte:0'],
            'limit'    => ['nullable', 'numeric', 'gte:1', 'lte:100'],
            'sort'     => ['nullable',],
            'order'    => ['nullable', 'string'],
            'title'    => ['nullable', 'string'],
            'start_at' => ['required_with:end_at', 'date_format:Y-m-d H:i:s', 'before_or_equal:end_at'],
            'end_at'   => ['required_with:start_at', 'date_format:Y-m-d H:i:s', 'after_or_equal:start_at'],
        ];
        $attributes = [
            'title' => '标题',
            // 'guard_name' => '对应规则',
        ];
        $messages   = [
            'name.string' => ':attribute必须为字符串',
        ];
        $validated  = $this->validate($request, $rules, $messages, $attributes);

        $data = $this->stockService->getList($validated);

        return $this->responseSuccessData('请求成功！', $data);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/24  16:13
     */
    public function create()
    {

        $data = $this->stockService->addData();
        return $this->responseSuccessData('创建成功！');
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * @Depiction 获取数据
     * @Author hwy <429208948@qq.com>
     * @Date 2022/5/20  13:43
     */
    public function getMarketData()
    {
        $data = [];
        if (class_exists('\App\Util\Stock')) {
            $data = (new \App\Util\Stock())->getMarketData();
        }

        return $this->responseSuccessData('获取成功！', $data);
    }

}
